package com.example.demo.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entify.Menu;
import com.example.demo.entify.RoleMenu;
import com.example.demo.entify.UserRole;
import com.example.demo.result.Result;
import com.example.demo.result.ResultFactory;
import com.example.demo.service.MenuService;

@RestController
@CrossOrigin
@RequestMapping("/menu")
public class MenuController {
	  @Autowired
	  private MenuService menuService;
	  
	 @GetMapping("/findAllmenu")	  
	  public Result findAllmenus(@RequestParam Map<String, Object> params) {
//		 	int pagesize = Integer.valueOf((String)params.get("currentPage"))* Integer.valueOf((String)params.get("pagesize"));
		 	int pagesize2 = (Integer.valueOf((String)params.get("currentPage"))-1)*Integer.valueOf((String)params.get("pagesize"));
//		 	params.put("pagesize",pagesize);
		 	params.put("pagesize2",pagesize2);
		  	List<Menu> menus  =menuService.findAllmenus(params);		 
		 return ResultFactory.buildSuccessFactory(menus);
		  
	  }
	 @PostMapping("/addroleMenu")	  
	  public Result addroleMenu(@RequestBody RoleMenu RoleMenu) {
		 boolean exist = menuService.isExistence(RoleMenu);		
		if (exist) {
			return ResultFactory.buildFailFactory("该角色已有该菜单");
		
		}else {
			int i=menuService.addroleMenu(RoleMenu);
			return i>0? ResultFactory.buildSuccessFactory(true) :ResultFactory.buildFailFactory("分配失败");		
		}
		  
	  }
	 @PostMapping("/deleteroleMenu")	  
	  public Result deleteroleMenu(@RequestBody RoleMenu RoleMenu) {
		int i=menuService.deleteRoleMenu(RoleMenu);		 
		 return i>0? ResultFactory.buildSuccessFactory(true) :ResultFactory.buildFailFactory("分配失败");		  
	  }
	 @PostMapping ("/addMenu")
	 public Result addMenu(@RequestBody Menu menu) {
		 	int i = menuService.addMenu(menu);
		 	return i>0 ? ResultFactory.buildSuccessFactory(true) :ResultFactory.buildFailFactory("添加失败");
		 		
		 		 
	 }
	 
	 
	 
	  
	
	
	
}
